/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the
 * confidential and proprietary information of Groupware.cc ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Groupware.cc.
 */
package com.rome.syncml.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.rome.syncml.bean.CallLog;
import com.rome.syncml.bean.CallLogVO;
import com.rome.syncml.constant.Constant;
import com.rome.syncml.domain.SyncItem;
import com.rome.syncml.persistence.SyncItemMapper;
import com.rome.syncml.util.CallLogUtil;

/**
 * 类CallLogService.java的实现描述：TODO 类实现描述 
 * @author afei 2011-11-26 下午01:55:27
 */
@Service
public class CallLogService {

    @Resource
    private SyncItemMapper syncItemMapper;
    
    
    private static String callLogType = "callLog";
    /**
     * 
     * @param userId
     * @param queryString
     * @param currentPage
     * @param pageSize
     * @param type 0:所有 1 来电 2 去电 3 未接
     * @return
     */
    public CallLogVO queryCallLog(long userId,String queryString,int currentPage,int pageSize,int type){
        int start = (currentPage-1)*pageSize;
        String condition = null;
        if(type!=Constant.CALLLOG_TYPE_ALL){
            condition = "\"type\":" + type; 
        }
        List<SyncItem> items = syncItemMapper.queryItems(userId, 1L, false, callLogType, queryString, condition, start, pageSize);
        CallLogVO vo = new CallLogVO();
        int count = syncItemMapper.countItems(userId, 1L, false, callLogType, queryString,condition);
        vo.setCount(count);
        vo.setCurrentPage(currentPage);
        vo.setPageSize(pageSize);
        vo.setTotalPage((count+pageSize-1)/pageSize);
        if(items != null){
            List<CallLog> list = new ArrayList<CallLog>();
            for(SyncItem item : items){
                CallLog log = CallLogUtil.parseCallLog(item.getContent());
                log.setFormatDate(new Date(log.getDate()));
                log.setId(item.getId());
                list.add(log);
            }
            vo.setLogs(list);
        }
        return vo;
        
    }
}
